﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LongSCADA_WPF.Helpers;
using LongSCADA_WPF.Models;
using LongSCADA_WPF.Services;
using LongSCADA_WPF.UseControlS;
using LongSCADA_WPF.Views;
using Masuit.Tools;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace LongSCADA_WPF.ViewModels
{
    public partial class UserViewModel:ObservableObject
    {
        public UserModelService UserModelService { get; }
        public UserViewModel(UserModelService userModelService)
        {
            UserModelService = userModelService;
        }

        [ObservableProperty]
        List<UserModel> _userList = new ();

        /// <summary>
        /// 窗体加载，初次查询
        /// </summary>
        [RelayCommand]
        void Load()
        {
            QueryUser();
        }
        [RelayCommand]
        void QueryUser()
        {
           UserList = SqlSugarHelper.Db.Queryable<UserModel>().ToList();
        }

        [RelayCommand]
        async void AddUser()
        {
            if (UserModelService.CurrentUserModel.Role!= 0)
            {
                UserModelService.ShowMessage($"{UserModelService.CurrentUserModel.UserName}-不是管理员,无权添加用户");
                return;
            }
            UserModel userModel = new UserModel();
            //接收存储 User 内容
            UserOperateView userOperateView = new UserOperateView { DataContext = new UpdateViewModel<UserModel>(userModel) };

            //ShellView.xaml 预留的 DialogHost在此此处显现
            var result = (bool)await DialogHost.Show(userOperateView, "ShellDialog");
            if (result)
            {
                int row = await SqlSugarHelper.Db.Insertable(userModel).ExecuteCommandAsync();
                if (row>0)
                {
                    QueryUser();
                    UserModelService.ShowMessage($"用户-{userModel.UserName}-添加成功");
                }
            }
        }
        [RelayCommand]
        async void EditUser(UserModel userModel)
        {
            if (UserModelService.CurrentUserModel.Role != 0)
            {
                UserModelService.ShowMessage($"{UserModelService.CurrentUserModel.UserName}-不是管理员,无权编辑用户");
                return;
            }

            //编辑用户，使用深 Clone，编辑弹窗关闭即修改信息--UpdateTime
            UserModel userDeep = userModel.DeepClone();
            //接收存储 User 内容
            UserOperateView userOperateView = new UserOperateView { DataContext = new UpdateViewModel<UserModel>(userDeep) };

            //ShellView.xaml 预留的 DialogHost在此此处显现
            var result = (bool)await DialogHost.Show(userOperateView, "ShellDialog");
            if (result)
            {
                userModel.UserName = userDeep.UserName;
                userModel.PassWord = userDeep.PassWord;
                userModel.Role = userDeep.Role;
                userModel.UpdateTime = DateTime.Now;

                int row = await SqlSugarHelper.Db.Updateable(userModel).ExecuteCommandAsync();
                if (row > 0)
                {
                    QueryUser();
                    UserModelService.ShowMessage($"用户-{userModel.UserName}-修改成功");
                }
            }
        }
        [RelayCommand]
        async void DeleteUser(UserModel userModel)
        {
            if (UserModelService.CurrentUserModel.Role != 0)
            {
                UserModelService.ShowMessage($"{UserModelService.CurrentUserModel.UserName}-不是管理员,无权删除用户");
                return;
            }
            if(userModel.Id == UserModelService.CurrentUserModel.Id)
            {
                UserModelService.ShowMessage($"{UserModelService.CurrentUserModel.UserName}-不能删除自己");
                return;
            }

            bool result = (bool)await DialogHost.Show(new DialogControl("确定要删除吗？", MessageBoxButton.YesNo), "ShellDialog");
            //MessageBoxResult result = UserModelService.ShowMessage("确定要删除吗", "删除提醒", MessageBoxButton.OKCancel);
            if (result)
            {
                int row = SqlSugarHelper.Db.Deleteable(userModel).ExecuteCommand();
                if (row>0)
                {
                    UserModelService.ShowMessage("删除成功了");
                    QueryUser();
                }
            }
        }
    }
}
